## Replication file for "Experimental Evidence on the Financial Consequences of International Organization Legitimacy" Part 2 ## 

## Clear all
rm(list = ls(all.names = TRUE))

## Set working directory
setwd("WORKING DIRECTORY")

## Load packages
library(readxl)
library(ggplot2)
library(dplyr)
library(gridExtra)
library(grid)       
library(gtable)     
library(scales)     


# Load data
## Output from "iolegitimacy-replication-1.R" 
charity_r1 <- read_excel("appendix_table_b1_b2.xlsx")
charity_r2 <- read_excel("appendix_table_b3.xlsx")


# (1) Experiment 1 
charity_r1 <- charity_r1 %>%
  filter(!test %in% c("Norm  (STC)", "Performance (STC)", "Procedural (STC)"))

## Treatment levels and labels
charity_r1$Treatment <- factor(charity_r1$test,
                              levels = c("Norm (UNICEF)",
                                         "Performance (UNICEF)",
                                         "Procedural (UNICEF)"),
                              labels = c("Mandate-Based Legitimacy",
                                         "Performance-Based Legitimacy",
                                         "Procedural-Based Legitimacy"))

charity_r1$country <- factor(charity_r1$country,
                            levels = c("Brazil",
                                       "Egypt",
                                       "India",
                                       "SaudiArabia",
                                       "UK"),
                            labels = c("Brazil",
                                       "Egypt",
                                       "India",
                                       "Saudi Arabia",
                                       "United Kingdom"))

pd <- position_dodge(width = 0.6)

x_limits <- c(-0.00055, 0.00055)

color_mapping <- c(
  "Mandate-Based Legitimacy" = "darkblue", 
  "Performance-Based Legitimacy" = "red",
  "Procedural-Based Legitimacy" = "orange")

plot_list <- list()
for (country_name in levels(charity_r1$country)) {
  data_subset <- filter(charity_r1, country == country_name)
  
  p <- ggplot(data_subset, aes(x = difference, y = Treatment, color = Treatment)) +
    geom_point(shape = 19, size = 2, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.96 * se, xmax = difference + 1.96 * se),
                  width = 0, size = 1, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.386 * se, xmax = difference + 1.386 * se),
                  width = 0.3, size = 1, position = pd) +
    scale_color_manual(values = color_mapping) +
    scale_x_continuous(limits = x_limits, labels = scales::label_number()) +
    theme_bw() +
    theme(axis.title.x = element_text(face = "bold", size = 14),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 12),
          axis.ticks.y = element_blank(),
          legend.position = "none",
          plot.margin = margin(0.2, 1.2, 0.2, 1.2, "cm"),
          plot.title = element_text(hjust = 0.5, size = 16, face = "bold")) +
    ylab("") +
    xlab("Difference in Clicks w/ Placebo") +
    ggtitle(country_name)
  
  plot_list[[country_name]] <- p
}


legend_only <- ggplot(charity_r1, aes(x = 1, y = 1, color = Treatment)) +
  geom_point() +
  scale_color_manual(values = color_mapping) +
  labs(color = "Treatment") + 
  theme_void() +
  theme(legend.position = "bottom",
        legend.text = element_text(size = 12),
        legend.key.size = unit(1, "cm"))

legend_grob <- ggplotGrob(legend_only)
legend_only_grob <- gtable_filter(legend_grob, "guide-box")

## 5 country plot (3 rows, 2 columns)
grid_layout <- grid.arrange(
  grobs = c(plot_list, list(legend_only_grob)),
  layout_matrix = rbind(
    c(1, 2),
    c(3, 4),
    c(5, NA),
    c(6, 6)  
  ),
  heights = c(1, 1, 1, 0.3)  
)

## Figure 2
tiff("figure2.tif", 
     width = 10, height = 8, units = "in", res = 300)
grid.draw(grid_layout)
dev.off()



# (2) Experiment 2
charity_r2$Treatment <- factor(charity_r2$test, 
                              levels = c("Norm",
                                         "Performance",
                                         "Procedural"),
                              labels = c("Mandate-Based Legitimacy",
                                         "Performance-Based Legitimacy",
                                         "Procedural-Based Legitimacy"))
charity_r2$country <- factor(charity_r2$country, 
                            levels = c("Egypt",
                                       "India"),
                            labels = c("Egypt",
                                       "India"))

pd <- position_dodge(width = 0.6)

x_limits <- c(-0.00055, 0.00055)

color_mapping <- c(
  "Mandate-Based Legitimacy" = "darkblue", 
  "Performance-Based Legitimacy" = "red", 
  "Procedural-Based Legitimacy" = "orange")

plot_list <- list()

for (country_level in levels(charity_r2$country)) {
  data_subset <- filter(charity_r2, country == country_level)
  
  p <- ggplot(data_subset, aes(x = difference, y = Treatment, color = Treatment)) +
    geom_point(shape = 19, size = 2, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.96 * se, xmax = difference + 1.96 * se),
                  width = 0, size = 1, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.386 * se, xmax = difference + 1.386 * se),
                  width = 0.3, size = 1, position = pd) +
    scale_color_manual(values = color_mapping) +
    scale_x_continuous(limits = x_limits, labels = scales::label_number()) +
    theme_bw() +
    theme(axis.title.x = element_text(face = "bold", size = 14),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 12),
          axis.ticks.y = element_blank(),
          legend.position = "none",  
          plot.margin = margin(0.2, 1.2, 0.2, 1.2, "cm"),  
          plot.title = element_text(hjust = 0.5, size = 16, face = "bold")) +
    ylab("") +
    xlab("Difference in Clicks w/ Placebo") +
    ggtitle(paste(country_level))
  
  plot_list[[country_level]] <- p
}

legend_only <- ggplot(charity_r2, aes(x = 1, y = 1, color = Treatment)) +
  geom_point() +
  scale_color_manual(values = color_mapping) +
  labs(color = "Treatment") + 
  theme_void() +
  theme(legend.position = "bottom",
        legend.text = element_text(size = 12),
        legend.key.size = unit(1, "cm"))

legend_grob <- ggplotGrob(legend_only)
legend_only_grob <- gtable_filter(legend_grob, "guide-box")

#  2 country plot
grid_layout <- grid.arrange(
  grobs = c(plot_list, list(legend_only_grob)),
  layout_matrix = rbind(
    c(1, 2),     
    c(3, 3)      
  ),
  heights = c(1, 0.3)  
)

# Figure 3
tiff("figure3.tif", 
     width = 10, height = 3, units = "in", res = 300)
grid.draw(grid_layout)
dev.off()



# (3) Survey experiment

## Load data
charity_survey <- read_excel("appendix_table_b4_b5_b6.xlsx")

## Filter 
charity_survey_intention <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-long) (Intention)",
                     "Norm (UNICEF-long) (Intention)",
                     "Performance (UNICEF-long) (Intention)"))

charity_survey_clicks <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-long) (Clicks)",
                     "Norm (UNICEF-long) (Clicks)",
                     "Performance (UNICEF-long) (Clicks)"))

charity_survey_winnings <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-long) (Winnings)",
                     "Norm (UNICEF-long) (Winnings)",
                     "Performance (UNICEF-long) (Winnings)"))

create_plots_with_legend <- function(data, x_label, output_file, x_limits) {
  pd <- position_dodge(width = 0.6)
  plot_list <- list()
  for (country_level in levels(data$country)) {
    data_subset <- filter(data, country == country_level)
    
    p <- ggplot(data_subset, aes(x = difference, y = Treatment, color = Treatment)) +
      geom_point(shape = 19, size = 2, position = pd) +
      geom_errorbar(aes(xmin = difference - 1.96 * se, xmax = difference + 1.96 * se),
                    width = 0, size = 1, position = pd) +
      geom_errorbar(aes(xmin = difference - 1.386 * se, xmax = difference + 1.386 * se),
                    width = 0.3, size = 1, position = pd) +
      scale_color_manual(values = color_mapping) +
      scale_x_continuous(limits = x_limits, labels = scales::label_number()) +
      theme_bw() +
      theme(axis.title.x = element_text(face = "bold", size = 14),
            axis.text.y = element_blank(),
            axis.text.x = element_text(size = 12),
            axis.ticks.y = element_blank(),
            legend.position = "none",
            plot.margin = margin(0.2, 1.2, 0.2, 1.2, "cm"),
            plot.title = element_text(hjust = 0.5, size = 16, face = "bold")) +
      ylab("") +
      xlab(x_label) +
      ggtitle(paste(country_level))
    
    plot_list[[country_level]] <- p
  }
  
  legend_only <- ggplot(data, aes(x = 1, y = 1, color = Treatment)) +
    geom_point() +
    scale_color_manual(values = color_mapping) +
    labs(color = "Treatment") +
    theme_void() +
    theme(legend.position = "bottom",
          legend.text = element_text(size = 12),
          legend.key.size = unit(1, "cm"))
  
  legend_grob <- ggplotGrob(legend_only)
  legend_only_grob <- gtable_filter(legend_grob, "guide-box")
  
  grid_layout <- grid.arrange(
    grobs = c(plot_list, list(legend_only_grob)),
    layout_matrix = rbind(
      c(1, 2),    
      c(3, 3)      
    ),
    heights = c(1, 0.15)  
  )
  
  tiff(output_file, width = 15, height = 5, units = "in", res = 300)
  grid.draw(grid_layout)
  dev.off()
  
  return(grid_layout)
}


## Clicks
charity_survey_clicks$Treatment <- factor(charity_survey_clicks$test,
                                         levels = c("Norm (UNICEF-long) (Clicks)",
                                                    "Performance (UNICEF-long) (Clicks)",
                                                    "Procedural (UNICEF-long) (Clicks)"),
                                         labels = c("Mandate-Based Legitimacy (Clicks)",
                                                    "Performance-Based Legitimacy (Clicks)",
                                                    "Procedural-Based Legitimacy (Clicks)"))

charity_survey_clicks$country <- factor(charity_survey_clicks$country,
                                       levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Clicks)" = "darkblue",
  "Performance-Based Legitimacy (Clicks)" = "red",
  "Procedural-Based Legitimacy (Clicks)" = "orange")

clicks_plot <- create_plots_with_legend(
  charity_survey_clicks,
  "Difference in Clicks w/ Placebo",
 "figure4_clicks.tif",
  c(-0.15, 0.15)
)


## Intention
charity_survey_intention$Treatment <- factor(charity_survey_intention$test,
                                            levels = c("Norm (UNICEF-long) (Intention)",
                                                       "Performance (UNICEF-long) (Intention)",
                                                       "Procedural (UNICEF-long) (Intention)"),
                                            labels = c("Mandate-Based Legitimacy (Intention)",
                                                       "Performance-Based Legitimacy (Intention)",
                                                       "Procedural-Based Legitimacy (Intention)"))

charity_survey_intention$country <- factor(charity_survey_intention$country,
                                          levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Intention)" = "darkblue",
  "Performance-Based Legitimacy (Intention)" = "red",
  "Procedural-Based Legitimacy (Intention)" = "orange")

intention_plot <- create_plots_with_legend(
  charity_survey_intention,
  "Difference in Intention w/ Placebo",
  "figure4_intention.tif",
  c(-0.15, 0.15)
)


## Winnings
charity_survey_winnings$Treatment <- factor(charity_survey_winnings$test,
                                           levels = c("Norm (UNICEF-long) (Winnings)",
                                                      "Performance (UNICEF-long) (Winnings)",
                                                      "Procedural (UNICEF-long) (Winnings)"),
                                           labels = c("Mandate-Based Legitimacy (Winnings)",
                                                      "Performance-Based Legitimacy (Winnings)",
                                                      "Procedural-Based Legitimacy (Winnings)"))

charity_survey_winnings$country <- factor(charity_survey_winnings$country,
                                         levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Winnings)" = "darkblue",
  "Performance-Based Legitimacy (Winnings)" = "red",
  "Procedural-Based Legitimacy (Winnings)" = "orange")

winnings_plot <- create_plots_with_legend(
  charity_survey_winnings,
  "Difference in Winnings w/ Placebo",
  "figure4_winnings.tif",
  c(-500, 500)
)


## Figure 4 (Combined plot)
extract_plots <- function(grid_layout) {
  g <- grid_layout$layout
  plot_rows <- g$t[g$t != max(g$t)]
  plot_cols <- unique(g$l[g$t %in% plot_rows])
  plots_only <- grid.arrange(
    grobs = grid_layout$grobs[1:2],  
    ncol = 2
  )
  return(plots_only)
}

tiff("figure4_combined.tif", width = 12, height = 10, units = "in", res = 300)

grid.arrange(
  clicks_plot,
  rectGrob(gp = gpar(col = NA)),  
  intention_plot,
  rectGrob(gp = gpar(col = NA)),  
  winnings_plot,
  ncol = 1,
  heights = c(0.9, 0.05, 0.9, 0.05, 0.9)  
)
dev.off()



# Appendix Figure G.2. (Short version)

## Load data
charity_survey <- read_excel("appendix_table_b4_b5_b6.xlsx")

## Filter 
charity_survey_intention_short <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-short) (Intention)",
                     "Norm (UNICEF-short)(Intention)",
                     "Performance (UNICEF-short)(Intention)"))

charity_survey_clicks_short <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-short) (Clicks)",
                     "Norm (UNICEF-short)(Clicks)",
                     "Performance (UNICEF-short)(Clicks)"))

charity_survey_winnings_short <- charity_survey %>%
  filter(test %in% c("Procedural (UNICEF-short) (Winnings)",
                     "Norm (UNICEF-short)(Winnings)",
                     "Performance (UNICEF-short)(Winnings)"))


## Clicks (short)
charity_survey_clicks_short$Treatment <- factor(charity_survey_clicks_short$test,
                                               levels = c("Norm (UNICEF-short)(Clicks)",
                                                          "Performance (UNICEF-short)(Clicks)",
                                                          "Procedural (UNICEF-short) (Clicks)"),
                                               labels = c("Mandate-Based Legitimacy (Clicks)",
                                                          "Performance-Based Legitimacy (Clicks)",
                                                          "Procedural-Based Legitimacy (Clicks)"))

charity_survey_clicks_short$country <- factor(charity_survey_clicks_short$country,
                                             levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Clicks)" = "darkblue",
  "Performance-Based Legitimacy (Clicks)" = "red",
  "Procedural-Based Legitimacy (Clicks)" = "orange")

clicks_plot_short <- create_plots_with_legend(
  charity_survey_clicks_short,
  "Difference in Clicks w/ Placebo",
  "appendix_figure_g2_clicks.tif",
  c(-0.1, 0.1)
)


## Intention (short)
charity_survey_intention_short$Treatment <- factor(charity_survey_intention_short$test,
                                                  levels = c("Norm (UNICEF-short)(Intention)",
                                                             "Performance (UNICEF-short)(Intention)",
                                                             "Procedural (UNICEF-short) (Intention)"),
                                                  labels = c("Mandate-Based Legitimacy (Intention)",
                                                             "Performance-Based Legitimacy (Intention)",
                                                             "Procedural-Based Legitimacy (Intention)"))

charity_survey_intention_short$country <- factor(charity_survey_intention_short$country,
                                                levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Intention)" = "darkblue",
  "Performance-Based Legitimacy (Intention)" = "red",
  "Procedural-Based Legitimacy (Intention)" = "orange")

intention_plot_short <- create_plots_with_legend(
  charity_survey_intention_short,
  "Difference in Intention w/ Placebo",
  "appendix_figure_g2_intention.tif",
  c(-0.1, 0.1)
)


## Winnings (short)
charity_survey_winnings_short$Treatment <- factor(charity_survey_winnings_short$test,
                                                 levels = c("Norm (UNICEF-short)(Winnings)",
                                                            "Performance (UNICEF-short)(Winnings)",
                                                            "Procedural (UNICEF-short) (Winnings)"),
                                                 labels = c("Mandate-Based Legitimacy (Winnings)",
                                                            "Performance-Based Legitimacy (Winnings)",
                                                            "Procedural-Based Legitimacy (Winnings)"))

charity_survey_winnings_short$country <- factor(charity_survey_winnings_short$country,
                                               levels = c("Egypt", "India"))

color_mapping <- c(
  "Mandate-Based Legitimacy (Winnings)" = "darkblue",
  "Performance-Based Legitimacy (Winnings)" = "red",
  "Procedural-Based Legitimacy (Winnings)" = "orange")

winnings_plot_short <- create_plots_with_legend(
  charity_survey_winnings_short,
  "Difference in Winnings w/ Placebo",
  "appendix_figure_g2_winnings.tif",
  c(-500, 500)
)


# Appendix Figure G.2.
tiff("appendix_figure_g2_combined.tif", width = 12, height = 10, units = "in", res = 300)
grid.arrange(
  clicks_plot_short,
  intention_plot_short, 
  winnings_plot_short,
  ncol = 1,
  heights = c(1, 1, 1)
)
dev.off()



# Appendix Figure G.1. (STC plot)

# Load data 
charity_r1 <- read_excel("appendix_table_b1_b2.xlsx")
charity_r1 <- charity_r1 %>%
  filter(!test %in% c("Norm (UNICEF)", "Performance (UNICEF)", "Procedural (UNICEF)"))

charity_r1$Treatment <- factor(charity_r1$test,
                              levels = c("Norm  (STC)",
                                         "Performance (STC)",
                                         "Procedural (STC)"),
                              labels = c("Mandate-Based Legitimacy (STC)",
                                         "Performance-Based Legitimacy (STC)",
                                         "Procedural-Based Legitimacy (STC)"))

charity_r1$country <- factor(charity_r1$country,
                            levels = c("Brazil",
                                       "Egypt",
                                       "India",
                                       "SaudiArabia",
                                       "UK"),
                            labels = c("Brazil",
                                       "Egypt",
                                       "India",
                                       "Saudi Arabia",
                                       "United Kingdom"))

color_mapping <- c(
  "Mandate-Based Legitimacy (STC)" = "darkblue",
  "Performance-Based Legitimacy (STC)" = "red",
  "Procedural-Based Legitimacy (STC)" = "orange")

plot_list <- list()

for (country_level in levels(charity_r1$country)) {
  data_subset <- filter(charity_r1, country == country_level)
  
  p <- ggplot(data_subset, aes(x = difference, y = Treatment, color = Treatment)) +
    geom_point(shape = 19, size = 2, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.96 * se, xmax = difference + 1.96 * se),
                  width = 0, size = 1, position = pd) +
    geom_errorbar(aes(xmin = difference - 1.386 * se, xmax = difference + 1.386 * se),
                  width = 0.3, size = 1, position = pd) +
    scale_color_manual(values = color_mapping) +
    scale_x_continuous(limits = c(-0.0005, 0.0005), labels = scales::label_number()) +
    theme_bw() +
    theme(axis.title.x = element_text(face = "bold", size = 14),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 12),
          axis.ticks.y = element_blank(),
          legend.position = "none",
          plot.margin = margin(0.2, 1.2, 0.2, 1.2, "cm"),
          plot.title = element_text(hjust = 0.5, size = 16, face = "bold")) +
    ylab("") +
    xlab("Difference in Clicks w/ Placebo") +
    ggtitle(paste(country_level))
  
  plot_list[[country_level]] <- p
}

legend_only <- ggplot(charity_r1, aes(x = 1, y = 1, color = Treatment)) +
  geom_point() +
  scale_color_manual(values = color_mapping) +
  labs(color = "Treatment") +
  theme_void() +
  theme(legend.position = "bottom",
        legend.text = element_text(size = 12),
        legend.key.size = unit(1, "cm"))

legend_grob <- ggplotGrob(legend_only)
legend_only_grob <- gtable_filter(legend_grob, "guide-box")

## 5 country plot
grid_layout <- grid.arrange(
  grobs = c(plot_list, list(legend_only_grob)),
  layout_matrix = rbind(
    c(1, 2),
    c(3, 4),
    c(5, NA),
    c(6, 6)  
  ),
  heights = c(1, 1, 1, 0.3)  
)


# Appendix Figure G.1
tiff("appendix_figure_g1.tif", width = 12, height = 8, units = "in", res = 300)
grid.draw(grid_layout)
dev.off()




